<template>
  <div class="contacts-list">
    <div v-if="loading" class="contacts-loading">
      <div class="loading-spinner"></div>
      <p>加载中...</p>
    </div>

    <div v-else-if="contacts.length === 0" class="contacts-empty">
      <div class="empty-icon">📭</div>
      <h3>暂无联系人</h3>
      <p>{{ emptyMessage }}</p>
    </div>

    <template v-else>
      <ContactItem
        v-for="contact in contacts"
        :key="contact.id"
        :contact="contact"
        :is-selected="selectedContactId === contact.id"
        @select="handleSelectContact"
        @send-message="handleSendMessage"
        @view-contact="handleViewContact"
        @call="handleCall"
        @video-call="handleVideoCall"
        @more-actions="handleMoreActions"
      />
    </template>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useContactStore } from '@/store/contact'
import ContactItem from './ContactItem/index.vue'
import type { Contact } from './types'

interface Emits {
  (e: 'select-contact', contact: Contact): void
  (e: 'send-message', contact: Contact): void
  (e: 'view-contact', contact: Contact): void
  (e: 'call', contact: Contact): void
  (e: 'video-call', contact: Contact): void
  (e: 'more-actions', contact: Contact): void
}

const emit = defineEmits<Emits>()

// 使用store获取数据
const contactStore = useContactStore()

// 从store获取数据
const contacts = computed(() => contactStore.filteredContacts)
const selectedContactId = computed(() => contactStore.selectedContactId)
const loading = computed(() => contactStore.isLoading)
const emptyMessage = '没有找到匹配的联系人，试试其他关键词吧'

const handleSelectContact = (contact: Contact): void => {
  emit('select-contact', contact)
}

const handleSendMessage = (contact: Contact): void => {
  emit('send-message', contact)
}

const handleViewContact = (contact: Contact): void => {
  emit('view-contact', contact)
}

const handleCall = (contact: Contact): void => {
  emit('call', contact)
}

const handleVideoCall = (contact: Contact): void => {
  emit('video-call', contact)
}

const handleMoreActions = (contact: Contact): void => {
  emit('more-actions', contact)
}
</script>

<style scoped>
.contacts-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 200px;
}

/* 加载状态 */
.contacts-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 24px;
  color: var(--text-color-secondary);
}

.loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border-color-light);
  border-top: 3px solid var(--primary-color);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 空状态 */
.contacts-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 24px;
  text-align: center;
  color: var(--text-color-secondary);
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.6;
}

.contacts-empty h3 {
  font-size: 18px;
  font-weight: 500;
  color: var(--text-color-primary);
  margin: 0 0 8px 0;
}

.contacts-empty p {
  font-size: 14px;
  margin: 0;
  opacity: 0.8;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .contacts-list {
    gap: 6px;
  }

  .contacts-empty,
  .contacts-loading {
    padding: 32px 16px;
  }

  .empty-icon {
    font-size: 36px;
  }
}

@media (max-width: 480px) {
  .contacts-list {
    gap: 4px;
  }

  .contacts-empty,
  .contacts-loading {
    padding: 24px 12px;
  }

  .empty-icon {
    font-size: 32px;
  }

  .contacts-empty h3 {
    font-size: 16px;
  }

  .contacts-empty p {
    font-size: 13px;
  }
}
</style>
